package com.funong.matrix.biz.user.facade.impl;

import cn.hutool.core.lang.tree.Tree;
import com.funong.matrix.biz.base.entity.SysUserEntity;
import com.funong.matrix.biz.power.domain.SysRoleUserDomainDao;
import com.funong.matrix.biz.power.pojo.dto.SysRoleUserDTO;
import com.funong.matrix.biz.user.domain.SysUserDomainDao;
import com.funong.matrix.biz.user.facade.SysUserFacade;
import com.funong.matrix.biz.user.pojo.dto.ResetPasswordDTO;
import com.funong.matrix.biz.user.pojo.dto.UserDTO;
import com.funong.matrix.biz.user.pojo.dto.UserLoginDTO;
import com.funong.matrix.biz.user.pojo.dto.UserPermissionMenuDTO;
import com.funong.matrix.biz.user.pojo.vo.LoginVO;
import com.funong.matrix.core.dto.QueryDTO;
import com.funong.matrix.core.dto.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author wanjialin
 * @version 1.0
 * @date 2023/5/18
 */

@Service
public class SysUserFacadeImpl implements SysUserFacade {

    @Resource
    private SysUserDomainDao sysUserDomainDao;

    @Resource
    private SysRoleUserDomainDao sysRoleUserDomainDao;

    @Override
    public Result queryUser(QueryDTO<UserDTO> queryDTO) {
        return sysUserDomainDao.queryUser(queryDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveUser(SysUserEntity sysUserEntity) {
        sysUserDomainDao.saveUser(sysUserEntity);

        if (sysUserEntity.getCustomerId() != null) {
            // 关联角色
            SysRoleUserDTO roleUserDTO = new SysRoleUserDTO();
            roleUserDTO.setUserId(sysUserEntity.getId());
            roleUserDTO.setRoleCodeList(Collections.singletonList("customer"));
            roleUserDTO.setSystemId(sysUserEntity.getSystemId());
            sysRoleUserDomainDao.saveUserRole(roleUserDTO);
        }

        return true;
    }

    @Override
    public Boolean deleteUser(Integer id) {
        return sysUserDomainDao.deleteUser(id);
    }

    @Override
    public SysUserEntity queryUserById(Integer id) {
        return sysUserDomainDao.queryUserById(id);
    }

    @Override
    public Boolean updateUser(SysUserEntity sysUserEntity) {
        return sysUserDomainDao.updateUser(sysUserEntity);
    }

    @Override
    public Boolean updatePassword(ResetPasswordDTO resetPasswordDTO) {
        return sysUserDomainDao.updatePassword(resetPasswordDTO);
    }

    @Override
    public LoginVO userLogin(UserLoginDTO userLoginDTO) {
        return sysUserDomainDao.userLogin(userLoginDTO);
    }

    @Override
    public SysUserEntity queryUserByAccount(String account) {
        return sysUserDomainDao.queryUserByAccount(account, null);
    }

    @Override
    public Boolean updateUserStatus(Integer id) {
        return sysUserDomainDao.updateUserStatus(id);
    }

    @Override
    public UserPermissionMenuDTO queryUserPremiseMenu(Integer id) {
        return sysUserDomainDao.queryUserPremiseMenu(id);
    }

    @Override
    public List<Tree<Integer>> querySelect() {
        return sysUserDomainDao.querySelect();
    }
}
